import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 144. 二叉树的前序遍历
 * @author: WXY
 * @create: 2023-01-02 15:22
 * @Version 1.0
 **/
public class num141_preorderTraversal {
    List<Integer> list = new ArrayList<Integer>();

    public List<Integer> preorderTraversal(TreeNode root) {
        //List<Integer> list = new ArrayList<>();
        if (root == null) return list;
        return preorder(root, list);
    }

    private List<Integer> preorder(TreeNode root, List<Integer> list) {
        if (root == null) return null;
        list.add(root.val);
        preorder(root.left, list);
        preorder(root.right, list);
        return list;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


}
